package com.qfedu.mtl.controller;


import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Orders;
import com.qfedu.mtl.query.OrderQuery;
import com.qfedu.mtl.service.OrderService;
import com.qfedu.mtl.service.impl.OrderServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version v1.0
 * @Date 2023/9/26 20:29
 * @Author 浪里小白条
 * @Email corday@qq.com
 */
@Controller
@RequestMapping("/Orders")
public class OrdersController {
    OrderService service=new OrderServiceImpl();
    //全部订单
    @RequestMapping("/pager.do")
    public void ManagersLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterUtil.setContentWithJson(req,resp);
//        //接收数据
        //分页查询
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        page  =  StrUtil.ifNull(page,"1");
        size = StrUtil.ifNull(size,"2");
        //接收查询条件
        OrderQuery orderQuery = HttpUtil.request2Bean(req, OrderQuery.class);
        //调用业务层代码

        Pager<Orders> pager=service.findPage(Integer.valueOf(page),Integer.valueOf(size),orderQuery);
        //根据页面对象创建vo
        TableVO<Orders> vo = TableVO.from(pager);
        //把数据转成json
        String json = JSON.toJSONString(vo);
        //输出
        resp.getWriter().print(json);
    }

    //    已完成
    @RequestMapping("/canCele.do")
    public void canceledOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    //    删除
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterUtil.setContentWithJson(req,resp);
        String orderId = req.getParameter("orderId");
        Integer back= service.deleteById(Integer.valueOf(orderId));
        if (back>0){
            HttpUtil.responseSuccess(null,resp);
        }else{
            HttpUtil.response(null,-1,"操作失败",resp);

        }

    }
    //    批量删除
    @RequestMapping("/deleteBatch.do")
    public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterUtil.setContentWithJson(req,resp);
        String ids = req.getParameter("ids");
        String[] idArray = ids.split(",");
        Integer sum=0;
        for (String s : idArray) {
            Integer integer = service.deleteById(Integer.valueOf(s));
            sum+=integer;
        }

        if (sum>0){
            HttpUtil.responseSuccess(null,resp);
        }else{
            HttpUtil.response(null,-1,"操作失败",resp);

        }

    }
    //    删除
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterUtil.setContentWithJson(req,resp);
        Orders orders = HttpUtil.request2Bean(req, Orders.class);
        Integer back= service.update(orders);
        if (back>0){
            HttpUtil.responseSuccess(null,resp);
        }else{
            HttpUtil.response(null,-1,"操作失败",resp);

        }

    }
}
